using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Box2D.XNA;
namespace TurbaconPhone
{
    public class KineticGameObject : GameObject
    {
        protected SoundEffect bumpSound;
        public Body Body { get; protected set; }
        public Fixture Fixture { get; protected set; }
        public override Vector2 Pos
        {
            get
            {
                return Body.Position*Physics.WorldScale;
            }
            set
            {
                _pos = value;
            }
        }
        private Vector2 _pos;
        public override Vector2 Vel
        {
            get
            {
                return Body.GetLinearVelocity() * Physics.WorldScale;
            }
            set
            {
                Body.SetLinearVelocity(value / Physics.WorldScale);
            }
        }
        public override float AngVel
        {
            get
            {
                return Body.GetAngularVelocity();
            }
            set
            {
                Body.SetAngularVelocity(value);
            }
        }
        public override float Angle
        {
            get
            {
                return Body.GetAngle();
            }
            set
            {
                orient = value;
            }
        }
        private float orient;
        public override float Scale
        {
            get
            {
                return scale ;
            }
            set
            {
                scale = value;
            }
        }
        private float scale;
        public KineticGameObject(string textureName, Vector2 pos)
            : base(textureName)
        {
            _pos = pos/Physics.WorldScale;
            BodyDef def = new BodyDef();
            def.position = _pos;
            def.type = BodyType.Dynamic;
            Body=Physics.World.CreateBody(def);
            Body.SetUserData(this);
            Color = Color.White;
            Scale = 1f;
            bumpSound = TurBacon.Content.GetSound("collide");
        }
        public void MakeCircle(float density)
        {
            CircleShape shape = new CircleShape();
            shape._radius = Texture.Width / Physics.WorldScale;
            Fixture = Body.CreateFixture(shape, density);
            
        }
        public void MakeBox(float density)
        {
            if (Texture != null)
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(Texture.Width / (2 * Physics.WorldScale), Texture.Height / (2 * Physics.WorldScale));
                Fixture = Body.CreateFixture(shape, density);
            }
        }
        public void MakeEdge(Vector2 v1, Vector2 v2, float density)
        {
            PolygonShape shape = new PolygonShape();
            shape.SetAsEdge(v1 / Physics.WorldScale, v2 / Physics.WorldScale);
            Fixture = Body.CreateFixture(shape, density);
        }
        public virtual void Bump(object obj)
        {
            
        }
        public override void Destroy()
        {
            Physics.World.DestroyBody(Body);
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }

        public void DebugDraw(SpriteBatch batch)
        {
            if (Fixture != null)
            {
                AABB aabb;
                Fixture.GetAABB(out aabb, 0);
                Vector2 c = aabb.GetCenter();
                Texture2D tex = TurBacon.Content.GetTexture("Ball");
                float r = aabb.GetExtents().X * Physics.WorldScale / Math.Max(tex.Width, tex.Height);
                batch.Draw(tex, c * Physics.WorldScale - new Vector2(tex.Width, tex.Height) / 2, null, Color.Green, 0, new Vector2(), r, SpriteEffects.None, 0);
            }
        }
        public bool Contains(Vector2 v)
        {
            AABB aabb;
            Fixture.GetAABB(out aabb, 0);
            Vector2 c = aabb.GetCenter() * Physics.WorldScale;
            Vector2 e = new Vector2(64); ;
            Vector2 top = c - e;
            Rectangle rect = new Rectangle((int)top.X, (int)top.Y, (int)e.X, (int)e.Y);
            return rect.Contains((int)v.X, (int)v.Y);
        }
    }
}
